﻿using SPDB.BTAP.Infrastructure.Data.DapperCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SPDB.BTAP.Domain;
using SPDB.BTAP.Infrastructure.Data;
using System.Data;
using System.Configuration;
using Dapper;

namespace SPDB.BTAP.WebLogic.BusinessTrip
{
    public class ServerBT_Approve : ExecuteSql<BT_Approve>
    {
        /// <summary>
        /// 出差审批列表查询
        /// </summary>
        /// <param name="approveEmplNo">审批人ID</param>
        /// <returns></returns>
        public List<BT_Approve> QuerByApproveEmplNo(string approveEmplNo)
        {
            return this.Query(a => a.ApproveEmplNo == approveEmplNo).ToList();
        }

        public BT_Approve GetOne(string Id)
        {
            List<BT_Approve> list = new List<BT_Approve>();
            list = this.Query(a => a.ID == Id).ToList();

            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 审批:通过
        /// </summary>
        /// <param name="ID">审批号</param>
        /// <param name="emplNo">审批人</param>
        /// <param name="approveDesc">审批意见</param>
        /// <param name="approveResult">审批意见</param>
        /// <returns>成功返回1,失败返回0</returns>
        public int Agree(string ID, string emplNo, string approveDesc = null, string approveResult = null)
        {
            return Submit(ID, emplNo, "3", approveDesc, approveResult);
        }

        /// <summary>
        /// 审批:拒绝
        /// </summary>
        /// <param name="ID">审批号</param>
        /// <param name="emplNo">审批人</param>
        /// <param name="approveDesc">审批意见</param>
        /// <param name="approveResult">审批意见</param>
        /// <returns>成功返回1,失败返回0/returns>
        public int Refuse(string ID, string emplNo, string approveDesc = null, string approveResult = null)
        {
            return Submit(ID, emplNo, "4", approveDesc, approveResult);
        }

        /// <summary>
        /// 审批:转批
        /// </summary>
        /// <param name="ID">审批号</param>
        /// <param name="emplNo">审批人</param>
        /// <param name="approveDesc">审批意见</param>
        /// <param name="approveResult">审批意见</param>
        /// <returns>成功返回1,失败返回0</returns>
        public int Transfer(string ID, string emplNo, string approveDesc = null, string approveResult = null)
        {
            using (IDbConnection conn = DataBaseAccess.CreateConnection())
            {
                int obj = conn.Execute("up_TransferApprove"
                    , new { ID = ID, EmplNo = emplNo, ApproveDesc = approveDesc, ApproveResult = approveResult }
                    , null
                    , null
                    , CommandType.StoredProcedure);
                if (obj > 0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 审批:通过/拒绝
        /// </summary>
        /// <param name="ID">审批号</param>
        /// <param name="emplNo">审批人</param>
        /// <param name="status">审批状态(审批通过3/审批拒绝4)</param>
        /// <param name="approveDesc">审批意见</param>
        /// <param name="approveResult">审批意见</param>
        /// <returns>成功返回1,失败返回0</returns>
        public int Submit(string ID, string emplNo, string status, string approveDesc = null, string approveResult = null)
        {
            using (IDbConnection conn = DataBaseAccess.CreateConnection())
            {
                int obj= conn.Execute("up_SubmitApprove"
                    , new { ID = ID, EmplNo = emplNo, Status = status, ApproveDesc = approveDesc, ApproveResult = approveResult }
                    , null
                    , null
                    , CommandType.StoredProcedure);
                if (obj > 0)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
                
            }
        }
    }
}